var $articleContent;
var $search;
var $tableFirstColumn;
var $tableOfEachRow;
var $borrowAndInformationButton;
var $borrowButton;
var $informationButton;
var $isBorrow = false;
var $mask;
var $returnMask;
var $cancelButton;
var $confirmButton;
var $borrowPanel;
var $returnPanel;
var $returnTitle;
var $returnDayInput;
var $informationConfirmbutton;
var $whichButton;

$(function() {
	$articleContent = $(".articleCenter .articleDisplay .content");
	$search = $(".articleCenter #search");
	$tableOfEachRow = $(".articleCenter table tr:not(:first)");
	$tableFirstColumn = $tableOfEachRow.find("td:first");
	$borrowAndInformationButton = $(".articleCenter table button");
	$borrowButton = $(".articleCenter table button:contains('点我借阅')");
	$informationButton = $(".articleCenter table button:contains('借出')");
	$mask = $(".articleCenter .mask");
	$returnMask = $(".articleCenter .returnMask");
	$cancelButton = $(".articleCenter .borrow input[type=button]");
	$confirmButton = $(".articleCenter input[type=submit]")
	$borrowPanel = $(".articleCenter .borrow");
	$returnPanel = $(".articleCenter .return");
	$returnTitle = $(".articleCenter .return .title");
	$returnDayInput = $(".articleCenter .borrow .returnDay");
	$informationConfirmbutton = $(".articleCenter button[type='button']");

	setArticleDisplayScroll();

	setSearchFocusListener();

	addBorrowButton();

	setBorrowButtonListener();
});

function setArticleDisplayScroll() {
	$articleContent.mCustomScrollbar({
		theme: "white",
		scrollButtons: {
			enable: false
		},
		autoHideScrollbar: true,
		scrollInertia: 100,
		horizontalScroll: false,
	});
}

function setSearchFocusListener() {
	$search.focus(function() {
		$search.velocity({
			width: "180px"
		}, 200, "ease");
		addSearchListener();
	});
	$search.blur(function() {
		$search.velocity({
			width: "100px"
		}, 200, "ease")
	});
}

function addSearchListener() {
	$search.on('input', function() {
		for (var i = 0; i < $tableFirstColumn.length; i++) {
			var $oneTd = $tableFirstColumn.get(i).innerHTML.toLowerCase();
			while ($oneTd.indexOf(" ") != -1) {
				$oneTd = $oneTd.replace(" ", "");
			}
			if ($oneTd.search($search.val().toLowerCase()) != -1) {
				$tableOfEachRow.get(i).style.display = "block";
			} else {
				$tableOfEachRow.get(i).style.display = "none";
			}
			var $array = $search.val().toLowerCase().split(" ");
			for (var j = 0; j < $array.length; j++) {
				if ($oneTd.search($array[j]) != -1) {
					if ($array[j] != "") {
						$tableOfEachRow.get(i).style.display = "block";
					}
				}
			}
		}
	});
	//	(function(event){//这种做法输入中文的时无反应
	//		setTimeout(function(){
	//			//当用户按下按键的时候，JavaScript 引擎需要执行 keydown 的事件处理程序，
	//			//然后更新文本框的 value 值，这两件事也需要按顺序来，事件处理程序执行时，更
	//			//新 value 值的任务则进入队列等待。所以我们在 keydown 的事件处理程序里是无
	//			//法得到更新后的 value 的，利用 setTimeout，我们把取 value 的操作放入队
	//			//列，放在更新 value 值以后，这样便达到了目的。
	//			$("title").html($search.val());
	//	}, 0);
};

function addBorrowButton() {
	$borrowAndInformationButton.hover(function() {
		if ($(this).html() == "借出") {
			$isBorrow = true;
			$(this).html("查看详情");
		} else {
			$isBorrow = false;
			$(this).html("借阅");
		}
	}, function() {
		if ($isBorrow) {
			$(this).html("借出");
		} else {
			$(this).html("点我借阅")
		}
	});
};

function setBorrowButtonListener() {

	$borrowButton.click(function() {
		$mask.fadeIn("fast")
		$borrowPanel.fadeIn("fast");
                $borrowPanel.find(".gid").val($("#DTOGid").val());
		$(".articleCenter .borrow .title").html("借阅：" + $(this).parent().parent().find("td:first").html())
		$returnDayInput.focus();
		$whichButton = $(this);
	});
	$returnDayInput.focus(function(){
		$(".articleCenter .borrow .borrowError").fadeOut("fast");
	});
	$confirmButton.click(function() {
		var re = /\d{4}-\d{1,2}-\d{1,2}/;
		var value = $returnDayInput.val();
		var isValid = re.exec(value);
		if (isValid != null) {
			$returnDayInput.val(isValid);
			return true;
		}else{
			$(".articleCenter .borrow .borrowError").fadeIn("fast");
			return false;
		}
	});
	
	$cancelButton.click(function() {
		$mask.fadeOut("fast");
		$borrowPanel.fadeOut("fast");
	});
	$mask.click(function() {
		$mask.fadeOut("fast");
		$borrowPanel.fadeOut("fast");
	});

	$informationButton.click(function() {
		
		linkInformation($(this));
		
		$returnMask.fadeIn("fast");
		$returnPanel.fadeIn("fast");
		$returnTitle.html($(this).parent().find("td:first").html());
	});
	$informationConfirmbutton.click(function() {
		$returnMask.fadeOut("fast");
		$returnPanel.fadeOut("fast");
	});
	$returnMask.click(function() {
		$returnMask.fadeOut("fast");
		$returnPanel.fadeOut("fast");
	});
	$cancelButton.click(function(){
		$mask.fadeOut("fast");
		$borrowPanel.fadeOut("fast");		
	})
}

function linkInformation($clickButton){
	var $tr = $clickButton.parent().parent();
	var $tableInf = $(".articleCenter .return table");
	$tableInf.find(".preditReturnTime").html($tr.find("td:nth-child(3)").html());
	$tableInf.find(".lendTime").html($tr.find("td:nth-child(4)").html());
	$tableInf.find(".borrower").html($tr.find("td:nth-child(5)").html());
	$tableInf.find(".shortNumber").html($tr.find("td:nth-child(6)").html());
	$tableInf.find(".QQNumber").html($tr.find("td:nth-child(7)").html());
	$tableInf.find(".longNumber").html($tr.find("td:nth-child(8)").html());
	$tableInf.find(".currentAddress").html($tr.find("td:nth-child(9)").html());
}
